package com.schedualnew.mq.yihuiesb.util;


import java.io.File;
import java.util.Map;
import java.util.Objects;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import cn.hutool.json.JSONUtil;
import com.schedualnew.mq.config.ApplyMQConfig;
import com.schedualnew.mq.service.MQClientService;
import lombok.extern.java.Log;
import org.apache.commons.lang.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.omg.CORBA.PUBLIC_MEMBER;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.stereotype.Component;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;


@Component
public class FieldConfigUtil {
    private Logger logger = LogManager.getLogger(FieldConfigUtil.class);
    private Resource resource = null;
    private Resource resourceTj = null;

    Element element = null;
    Element elementTj = null;

    @Autowired
    private ApplyMQConfig applyMQConfig;


    public FieldConfigUtil() {
        //loadConfigXml();
    }


    public void loadConfigXml() {
        try {

            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            dbf.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
            DocumentBuilder db = dbf.newDocumentBuilder();

            try {
                resource = new ClassPathResource("/config/fieldConfig.xml");
                resourceTj = new ClassPathResource("/config/fieldConfigTj.xml");

                Document document = db.parse(resource.getInputStream());
                element = document.getDocumentElement();
                logger.info("ClassPathResource=success");
            } catch (Exception e) {
                logger.error("loadResource:", e);

                logger.info("fieldConfig.xml:{}", applyMQConfig.getMapFilePath());
                Document document = db.parse(new File(applyMQConfig.getMapFilePath()));
                element = document.getDocumentElement();
            }

            logger.info("element:{}", JSONUtil.toJsonStr(element));
        } catch (Exception e) {
            logger.error("loadConfigXml:", e);
        }
    }

    public String getElementStr() {
        if (Objects.isNull(element)) {
            loadConfigXml();
        }
        return JSONUtil.toJsonStr(element);

    }


    public String getValue(boolean isTj, String tableName, String fieldName) {
        String value = "";
        if (StringUtils.isNotEmpty(tableName) && StringUtils.isNotEmpty(fieldName)) {
            NodeList tableNameList;

            if (isTj) {
                tableNameList = elementTj.getElementsByTagName(tableName);
            } else {
                if (Objects.isNull(element)) {
                    // 重新加载一边配置文件
                    loadConfigXml();
                    logger.info("getValue:loadConfigXml");
                }
                tableNameList = element.getElementsByTagName(tableName);
            }
            if (tableNameList != null && tableNameList.getLength() > 0) {
                Element tableElement = (Element) tableNameList.item(0);
                NodeList fieldNameList = tableElement.getElementsByTagName(fieldName);

                if (fieldNameList != null && fieldNameList.getLength() > 0) {
                    Element node = (Element) fieldNameList.item(0);
                    value = node.getTextContent();
                }
            }
        }
        return value;
    }


    public String getFieldValue(boolean isTj, String tableName, String fieldName, Map<String, String> infoMap) {
        String result = null;
        String fieldKey = getValue(isTj, tableName, fieldName);
        if (!StringUtils.isEmpty(fieldKey)) {
            result = infoMap.get(fieldKey);
        }
        logger.info("key:{} value:{}", fieldKey, result);
        return result;
    }
}